package com.xlong.wljxprinter;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.spd.lib.mvp.BaseMvpActivity;
import com.spd.print.jx.constant.PrintConstant;
import com.spd.print.jx.inter.IConnectCallback;
import com.spd.print.jx.utils.ToastUtil;
import com.xlong.wljxprinter.application.BaseApp;
import com.xlong.wljxprinter.presenter.MainPresenter;

public class MainActivity extends BaseMvpActivity<MainPresenter> implements View.OnClickListener, IConnectCallback {

    private FloatingActionButton fab;
    private TextView qrcode;

    @Override
    protected int getActLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        qrcode = findViewById(R.id.qrcode);
        findViewById(R.id.floatingActionButton).setOnClickListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.floatingActionButton:
                //扫描二维码
                new IntentIntegrator(this)
                        .setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)// 扫码的类型,可选：一维码，二维码，一/二维码
                        //.setPrompt("请对准二维码")// 设置提示语
                        .setCameraId(0)// 选择摄像头,可使用前置或者后置
                        .setBeepEnabled(true)// 是否开启声音,扫完码之后会"哔"的一声
                        .initiateScan();// 初始化扫码
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //扫码结果
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (intentResult != null) {
            if (intentResult.getContents() == null) {
                //扫码失败
            } else {
                String result = intentResult.getContents();//返回值
                qrcode.setText("扫码结果：" + result);
            }
        }
    }


    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }

    @Override
    public void onPrinterConnectSuccess() {
        BaseApp.isConnection = true;

        //开启缺纸检测
//        CheckPrinterStatus.getInstance(mContext).startCheck();
    }

    @Override
    public void onPrinterConnectFailed(int errorCode) {
        //关闭缺纸检测
//        CheckPrinterStatus.getInstance(mContext).stopCheck();
        BaseApp.isConnection = false;
        BaseApp.deviceName = getResources().getString(R.string.status_disconnect);
        BaseApp.deviceAddress = getResources().getString(R.string.status_disconnect);
        switch (errorCode) {
            case PrintConstant.CONNECT_CLOSED:
                //ToastUtil.customToastView(mContext, getString(R.string.toast_close), Toast.LENGTH_SHORT
                //        , (TextView) LayoutInflater.from(mContext).inflate(R.layout.layout_toast, null));
                break;
            default:
                //ToastUtil.customToastView(mContext, getString(R.string.toast_fail), Toast.LENGTH_SHORT
                //        , (TextView) LayoutInflater.from(mContext).inflate(R.layout.layout_toast, null));
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (BaseApp.isConnection) {
            //btnConnect.setText(getResources().getString(R.string.disconnect_printer));
        } else {
            //btnConnect.setText(getResources().getString(R.string.connect_printer));
        }
        //statusName.setText(BaseApp.deviceName);
        //statusAddress.setText(BaseApp.deviceAddress);
    }

    @Override
    protected void onDestroy() {
        mPresenter.disconnectPrinter();
        super.onDestroy();
    }
}
